package
{
	import com.greensock.TweenLite;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	import utils.Button;
	import utils.Debuger;
	import utils.Res;
	import utils.TextButton;
	import utils.UIHelper;
	import utils.events.MenuEvent;

	public class MainMenuItem extends Sprite
	{
		private var _MainMenuItemName:String;
		private var _menubtn:TextButton;
		private var _contentContainer:Sprite;
		private var menuContentX:Number=0;
		private var menuContentY:Number=221;
		public var boolShowContent:Boolean;
		private var contentWidth:int;
		private var boolTweenLite:Boolean=false;

		public function MainMenuItem(menuName:String, downUrl:Class, overUrl:Class, contentContainer:Sprite)
		{
			this._MainMenuItemName=menuName;
			this.name=menuName;
			_menubtn=UIHelper.addBitmapDataTextButtonChild(this, menuName, downUrl, overUrl, true, onBtnClickHandler);
			//初始化打开合起的指示图标			

			if (contentContainer)
			{
				this._contentContainer=contentContainer;
			}
			else
			{
				_contentContainer=new Sprite();
			}
			this._contentContainer.name=menuName + "_contentContainer";
			this.contentWidth=this._contentContainer.width;
			//this._contentContainer.width=0;
			this.addEventListener(MenuEvent.onDeskTableCloseClick, onDeskTableCloseClickHandler);

		}

		protected function onDeskTableCloseClickHandler(event:MenuEvent):void
		{
			// TODO Auto-generated method stub
			trace("call onDeskTableCloseClickHandler!");
			if (boolShowContent)
			{
				if (this.parent && this.parent.parent.contains(this._contentContainer))
				{
					TweenLite.to(this._contentContainer, 1, {alpha: 0});
						//this._contentContainer.visible=false;
						//TweenLite.to(this._contentContainer,1, {width:0});
						//this.parent.parent.removeChild(this._contentContainer);			
				}
				this.parent.parent["curentContent"]=null;
			}
			boolShowContent=!boolShowContent;
		}

		public function get MainMenuItemName():String
		{
			return _MainMenuItemName;
		}

		public function set MainMenuItemName(value:String):void
		{
			_MainMenuItemName=value;
		}

		public function get menubtn():TextButton
		{
			return _menubtn;
		}

		public function set menubtn(value:TextButton):void
		{
			_menubtn=value;
		}

		public function get contentContainer():Sprite
		{
			return _contentContainer;
		}

		public function set contentContainer(value:Sprite):void
		{
			_contentContainer=value;
		}


		protected function onBtnClickHandler(event:MouseEvent):void
		{
			//Debuger.print("main menu onBtnClickHandler:" + this._MainMenuItemName);
			var boolwait:Boolean=false;
			if (this.parent)
			{
				var preMainmenu:MainMenuItem=this.parent.parent["curentContent"] as MainMenuItem;
				if (preMainmenu && preMainmenu != this)
				{
					boolwait=true;
					trace("需要等待：" + preMainmenu.MainMenuItemName + " !" + this._MainMenuItemName + " " + (boolShowContent ? "关闭" : "打开"));
					preMainmenu.boolShowContent=!preMainmenu.boolShowContent;
					TweenLite.to(preMainmenu.contentContainer, 1, {alpha: 0, onComplete: doCurrentMenuEase});

						//child.visible=false;
						//TweenLite.to(child,1, {width:0});
						//this.parent.parent.removeChild(child);		
						//Debuger.print("main menu onBtnClickHandler:remove " +preMainmenuName+"_contentContainer");
				}
				this.parent.parent["curentContent"]=null;
			}
			if (!boolwait)
			{
				trace("不需要等待：" + this._MainMenuItemName + " " + (boolShowContent ? "关闭" : "打开"));
				doCurrentMenuEase();
			}
		}

		private function doCurrentMenuEase():void
		{
			// TODO Auto Generated method stub
			//如果是显示，那么就隐藏
			if (boolShowContent)
			{
				if (this.parent && this.parent.parent.contains(this._contentContainer))
				{
					TweenLite.to(this._contentContainer, 1, {alpha: 0});
						//this._contentContainer.visible=false;
						//TweenLite.to(this._contentContainer,1, {width:0});
						//this.parent.parent.removeChild(this._contentContainer);			
				}
				this.parent.parent["curentContent"]=null;
			}
			else
			{
				if (this.parent && !this.parent.parent.contains(this._contentContainer))
				{
					this.parent.parent.addChild(this._contentContainer);

				}
				TweenLite.to(this._contentContainer, 1, {alpha: 1});
				//this._contentContainer.visible=true;
				//TweenLite.to(this._contentContainer,1, {width:this.contentWidth});
				this.parent.parent["curentContent"]=this;
			}
			boolShowContent=!boolShowContent;
		}

	}
}
